/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.examples.enhancedads.settings;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * The Class AccountSettings represents the AdWords Accounts to be processed by
 * EnhancedAds.
 * 
 * @author rodrigo.f.vale@gmail.com
 *  
 */
public class AccountSettings {
  private String login;
  private String password;
  private String clientEmail;  
  private String xml;
  
  public AccountSettings(String login, String password, String clientEmail, String xml) 
  {
    this.login = login;
    this.password = password;
    this.clientEmail = clientEmail;
    this.xml = xml;
  }
  
  public void setLogin(String login) {
    this.login = login;
  }
  
  public String getLogin() {
    return login;
  }
  
  public void setPassword(String password) {
    this.password = password;
  }
  
  public String getPassword() {
    return password;
  }
  
  public void setClientEmail(String clientEmail) {
    this.clientEmail = clientEmail;
  }
  
  public String getClientEmail() {
    return clientEmail;
  }
  
  public String getKey() {
    String key = login + "+" + clientEmail;
    return MD5(key);
  }

  public void setXml(String xml) {
    this.xml = xml;
  }

  public String getXml() {
    return xml;
  }  
  
  public static AccountSettings fromJSON(JSONObject jsonAccount) throws JSONException {
    String login = jsonAccount.getJSONObject("Login").getString("value");
    String password 
        = jsonAccount.getJSONObject("Password").getString("value");
    String clientLogin 
        = jsonAccount.getJSONObject("ClientLogin").getString("value");
    String xml 
      = jsonAccount.getJSONObject("XML").getString("value");
    
    return new AccountSettings(login, password, clientLogin, xml);
  }
  
  public String MD5(String input) {    
    MessageDigest md;
    StringBuffer sb = new StringBuffer();
    try {
      md = MessageDigest.getInstance("MD5");
      md.update(input.getBytes());
      
      byte byteData[] = md.digest();

      //convert the byte to hex format method 1
      for (int i = 0; i < byteData.length; i++) {
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
            .substring(1));
      }
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return sb.toString();
  }
}